from django import http
from . import const
from django.shortcuts import render

# Create your views here.
from django.views import View
from django_redis import get_redis_connection

from meiduo.libs.captcha.captcha import captcha


class ImageCodeView(View):
    """图形验证码"""
    def get(self, request, uuid):
        # 1.生成图形验证码：txt+image
        text,image = captcha.generate_captcha()
        # 2.获取redis的链接对象
        redis_conn=get_redis_connection('verify_code')
        # 3.保存验证码
        # redis_conn.setex(key, expire_time, value)
        redis_conn.setex('img_%s'%uuid , const.IMAGE_CODE_REDIS_EXPIRES ,text)
        # 4.返回图片验证码
        return http.HttpResponse( image , content_type='image/jpg')